from django.shortcuts import render
from django.http import HttpRequest,HttpResponse,HttpResponseBadRequest
# Create your views here.
print("Loading judgers...")
from .judgers.all_judgers import judgers,judge
print("Done.")

def main(req:HttpRequest):
    return HttpResponse(content="OK")

from .models import Submission,Problem
import json

def reset(req:HttpRequest):
    Submission.objects.all().delete()
    return HttpResponse("Deleted")

def add_problem(req:HttpRequest):
    if req.method!="POST":
        return HttpResponseBadRequest("Not supported request method")
    data=json.loads(req.body)
    pro=Problem()
    pro.problem=data["problem"]
    pro.type=data["type"]
    pro.save()
    return HttpResponse("OK")

def submit(bdy):
    sub=Submission()
    sub.real_id=bdy["submission_id"]
    sub.problem=bdy["problem"]
    sub.body=bdy["body"]
    sub.save()
    judge(sub.real_id)

def lsubmit(req:HttpRequest):
    if req.method!="POST":
        return HttpResponseBadRequest("Not supported request method")
    data=json.loads(req.body)
    submit(data)
    return HttpResponse("OK")

def check(req:HttpRequest):
    return HttpResponse("PONG")